package cn.loveq.android.widget;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.waps.AdInfo;

/**
 * 用于自定义广告列表的Adapter
 */
public class AdAdapter extends BaseAdapter {

	Context context;
	List<AdInfo> list;

	public AdAdapter(Context context, List<AdInfo> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout r_layout;
		ImageView app_icon;
		TextView app_name;
		RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		AdInfo adInfo = list.get(position);
		r_layout = new RelativeLayout(context);
		app_icon = new ImageView(context);
		app_icon.setId(1);
		app_name = new TextView(context);

		app_icon.setLayoutParams(new LayoutParams(75, 75));
		app_icon.setScaleType(ImageView.ScaleType.FIT_CENTER);

		rlp.addRule(RelativeLayout.RIGHT_OF, app_icon.getId());
		rlp.addRule(RelativeLayout.CENTER_VERTICAL);
		app_icon.setImageDrawable(new BitmapDrawable(adInfo.getAdIcon()));
		app_icon.setPadding(5, 5, 5, 5);

		app_name.setText(adInfo.getAdName());
		app_name.setTextSize(18);
		app_name.setTextColor(Color.WHITE);
		app_name.setPadding(10, 0, 0, 0);

		TextView content = new TextView(context);
		content.setText(adInfo.getAdText());
		content.setPadding(10, 0, 0, 0);

		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);

		layout.addView(app_name);
		layout.addView(content);

		r_layout.addView(app_icon);
		r_layout.addView(layout, rlp);

		convertView = r_layout;
		convertView.setTag(r_layout);
		return r_layout;
	}
}